/*
 * @(#)XfvhdlAntecedentMemComplementaryFiles.java   3.0      2004/09/14
 *
 * This file is part of Xfuzzy 3.0, a design environment for fuzzy logic
 * based systems.
 *
 * (c) 2000 IMSE-CNM. The authors may be contacted by the email address:
 *                    xfuzzy-team@imse.cnm.es
 *
 * Xfuzzy is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation.
 *
 * Xfuzzy is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

package xfuzzy.xfvhdl;

import xfuzzy.lang.*;

/**
* Clase que genera los ficheros complementarios de las memorias de 
* antecedentes.
* @author Jos� Mar�a �vila Maireles, <b>e-mail</b>: josavimai@alum.us.es
* @version 3.0
*/
public class XfvhdlAntecedentMemComplementaryFiles {

   //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
   //			  M�TO_DOS P�BLICOS DE LA CLASE				     
   //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//

   /**
   * M�todo que genera los ficheros complementarios de la memoria de 
   * antecedentes.
   */
   public void createCF(Specification spec) {

      int actual = 0;
      Variable[] var = spec.getSystemModule().getInputs();
      int total = var.length;
      XfvhdlUniverse u = new XfvhdlUniverse();
      XfvhdlDiscretizeUniverse disc = new XfvhdlDiscretizeUniverse();
      new XfvhdlError();

      // Para todas las variables (******incluidas las INNER*********)
      while (actual < total) {
         // Discretizo el universo de discurso para la variable actual
         u = disc.discretizeUniverse(var[actual]);

         // Generamos un fichero con lo que hay en la memoria de 
         // antecedentes para cada variable. 
         String valor = new String();

         for (int t = 0; t < u.getLength(); t++) {
            valor += "\n"
               + t
               + "   "
               + u.getXUniversePoint(t).getVal1()
               + "   "
               + u.getXUniversePoint(t).getVal2();
         }

         new XfvhdlCreateFile(
            XfvhdlProperties.outputFile
               + "valor_"
               + var[actual].getName()
               + ".dat",
            valor);

         // Generamos un fichero para CNUPlot que representa lo que hay 
         // en la memoria de antecedentes para cada variable.
         new XfvhdlCreateFile(
            XfvhdlProperties.outputFile
               + "var_"
               + var[actual].getName()
               + ".plt",
            createGnuPlotFile(var[actual].getName()));

         // Ahora volvemos a generar el mismo fichero otra vez, pero 
         // esta vez en binario:
         XfvhdlBinaryDecimal converter = new XfvhdlBinaryDecimal();
         String val1 = new String();
         String val2 = new String();
         String code = new String();

         for (int i = 0; i < u.getLength(); i++) {
            val1 =
               converter.toBinaryInRange(
                  u.getXUniversePoint(i).getVal1(),
                  0.0,
                  1.0,
                  XfvhdlProperties.grad);
            val2 =
               converter.toBinaryInRange(
                  u.getXUniversePoint(i).getVal2(),
                  0.0,
                  1.0,
                  XfvhdlProperties.grad);

            code += ""
               + converter.toBinary(
                  (u.getXUniversePoint(i).getEtiq()) - 1,
                  XfvhdlProperties.bits_etiq)
               + " "
               + val1
               + " "
               + val2
               + "\n";
         }

         new XfvhdlCreateFile(
            XfvhdlProperties.outputFile
               + "valor_"
               + var[actual].getName()
               + ".dat.bin",
            code);

         actual++;
      }
   }

   //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
   //			  M�TO_DOS PRIVADOS DE LA CLASE				        
   //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//

   /**
   * M�todo que crea la cadena que ser� escrita en fichero para que 
   * GNUPlot pueda representar una gr�fica.
   * @return Devuelve la cadena que ser� escrita en fichero .plt.
   */
   private String createGnuPlotFile(String v) {
      String code =
         "set terminal windows color \"Arial\" 10\n"
            + "set output\n"
            + "set noclip points\n"
            + "set clip one\n"
            + "set noclip two\n"
            + "set border\n"
            + "set boxwidth\n"
            + "set dummy x,y\n"
            + "set format x \"%g\"\n"
            + "set format y \"%g\"\n"
            + "set format z \"%g\"\n"
            + "set nogrid\n"
            + "set key\n"
            + "set nolabel\n"
            + "set noarrow\n"
            + "set nologscale\n"
            + "set offsets 0, 0, 0, 0\n"
            + "set nopolar\n"
            + "set angles radians\n"
            + "set noparametric\n"
            + "set view 60, 30, 1, 1\n"
            + "set samples 100, 100\n"
            + "set isosamples 10, 10\n"
            + "set surface\n"
            + "set nocontour\n"
            + "set clabel\n"
            + "set nohidden3d\n"
            + "set cntrparam order 4\n"
            + "set cntrparam linear\n"
            + "set cntrparam levels auto 5\n"
            + "set cntrparam points 5\n"
            + "set size 1,1\n"
            + "set data style points\n"
            + "set function style lines\n"
            + "set xzeroaxis\n"
            + "set yzeroaxis\n"
            + "set tics in\n"
            + "set ticslevel 0.5\n"
            + "set xtics\n"
            + "set ytics\n"
            + "set ztics\n"
            + "set title \"\" 0,0\n"
            + "set notime\n"
            + "set rrange [0 : 10]\n"
            + "set trange [-5 : 5]\n"
            + "set urange [-5 : 5]\n"
            + "set vrange [-5 : 5]\n"
            + "set xlabel \"\" 0,0\n"
            + "set xrange [0 : 31]\n"
            + "set ylabel \"\" 0,0\n"
            + "set yrange [0 : 1]\n"
            + "set zlabel \"\" 0,0\n"
            + "set zrange [-10 : 10]\n"
            + "set autoscale r\n"
            + "set autoscale t\n"
            + "set autoscale xy\n"
            + "set autoscale z\n"
            + "set zero 1e-08\n"
            + "plot '"
            + XfvhdlProperties.outputFile
            + "valor_"
            + v
            + ".dat' title 'grado n','"
            + XfvhdlProperties.outputFile
            + "valor_"
            + v
            + ".dat' using 1:3 title 'grado n+1'";

      return code;
   }

} // Fin de la clase
